#ifndef __LOG_H__
#define __LOG_H__

#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef struct{
	char name[32];
	int32_t level;
} log_level_t;

enum { FATAL, ALERT, CRIT, ERROR, WARN, NOTICE, INFO, DEBUG, TRACE, MAX_LOG_LEVEL };

const char log_strlevel[MAX_LOG_LEVEL][30]={
	"FATAL",
	"ALERT",
	"CRIT",
	"ERROR",
	"WARN",
	"NOTICE",
	"INFO",
	"DEBUG",
	"TRACE"
};

extern uint32_t log_currlevel;

#define PRINTF(debug, ...) {time_t tval; struct tm tt; localtime_r(&(tval = time(0) ), &tt );printf("%.2d:%.2d:%.2d\t"debug, tt.tm_hour, tt.tm_min, tt.tm_sec, ##__VA_ARGS__ );};
//#define PRINTF( debug, ...) { printf(debug, ##__VA_ARGS__ );};
#define LOG( level, debug, ... ) { if(level <= log_currlevel ) PRINTF( "%s\t"debug"\n", log_strlevel[ level ], ##__VA_ARGS__ );fflush( stdout ); }

void log_init();
uint8_t log_setlevel( uint8_t level );
uint8_t log_getlevel( char *nivel );

#define	LOG_FATAL( ... ) LOG( FATAL, ##__VA_ARGS__ ) 
#define LOG_ALERT( ... ) LOG( ALERT, ##__VA_ARGS__ ) 
#define LOG_CRIT( ... ) LOG( CRIT, ##__VA_ARGS__ ) 
#define LOG_ERROR( ... ) LOG( ERROR, ##__VA_ARGS__ ) 
#define LOG_WARN( ... ) LOG( WARN, ##__VA_ARGS__ ) 
#define LOG_NOTICE( ... ) LOG( NOTICE, ##__VA_ARGS__ ) 
#define LOG_INFO( ... ) LOG( INFO, ##__VA_ARGS__ ) 
#define LOG_DEBUG( ... ) LOG( DEBUG, ##__VA_ARGS__ ) 
#define LOG_TRACE( ... ) LOG( TRACE, ##__VA_ARGS__ ) 

#endif
